home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / dnet / dshterm1_0.lha / lib / st / fpf.s < prev    next >
Encoding:
Text File  |  1992-02-04  |  4.5 KB  |  141 lines

  1. ********************************************************************
  2. ; PRINTF REPLACEMENT ROUTINES
  3. ********************************************************************
  4. ; Written by Stone, SST, 1993 Email c9107253@mystra.newcastle.edu.au
  5. ; All source public domain of course
  6. ;
  7. ; BUGS: must use %ld & %lc instead of %d & %c due to long word
  8. ; passing
  9. ;
  10. ********************************************************************
  11.  
  12. ;    MODULE  RawDoFmtGlue
  13.     OPT        o+            ;optimize
  14.     OPT        l+            ;link
  15.  
  16.     xdef        _fpf        ;fprintf replacement: output, string, args...
  17.     xdef        _spf        ;sprintf replacement: outstring, maxn, string, args...
  18.     xref        _DOSBase    ;relative to A4!
  19.  
  20.     include "dh0:stone/dazsys/macros.i"
  21.     include "dh0:stone/dazsys/xrefs"
  22.     
  23.  
  24. ********************************************************************
  25. _fpf ; call with output, string, args, ...
  26. *********************************************************************
  27. ; DO A FORMATTED PRINT SIMILAR TO fprintf EXCEPT USE EXEC RawDoFmt FOR SMALLNESS!
  28. ; NUMBER OF CHARACTERS ACTUALLY PRINTED IS RETURNED IN D0
  29. BUFSIZE = 120
  30.                 rsset        0
  31. pf_msgbuf        rs.b    BUFSIZE                        ;MUST be first! (and divi by 4)
  32. pf_DOSBase        rs.l    1                            ;need this for printing!
  33. pf_stringpos    rs.w    1                            ;current position
  34. pf_totprint        rs.w    1                            ;total printed
  35. pf_badoutput    rs.b    1                            ;output was bad
  36. pf_pad            rs.b    3
  37. pf_noofvars        rs.l    0
  38. pf_stacked        rs.l    14
  39. pf_return        rs.l    1
  40. ; CALLER MUST SUPPLY THE FOLLOWING ON THE STACK
  41. pf_output        rs.l    1                            ;output file
  42. pf_string        rs.l    1                            ;format
  43. pf_stream        rs.l    0                            ;start of the stream
  44. ; also: a4 should point at global variable table with _DOSBase defined
  45. *********************************************************************
  46.         movem.l        d1-d7/a0-a6,-(sp)
  47.         lea            (0-pf_noofvars)(sp),sp
  48.         moveq        #0,d0                            ;for return
  49.         if.l        not,pf_output(sp),_fpf_thatsall    ;NULL file?
  50.         move.l        _DOSBase(a4),pf_DOSBase(sp)        ;get the DOSBase
  51.         move.l        sp,a3                            ;a3 <== base of data
  52.         clr.b        pf_badoutput(sp)
  53.         clr.w        pf_stringpos(sp)                ;buf pos
  54.         clr.w        pf_totprint(sp)                    ;for return
  55.         move.l        pf_string(sp),a0
  56.         lea            pf_stream(sp),a1
  57.         lea            P_DoChar(pc),a2                    ;call this each char
  58.         LVOEXEC        RawDoFmt
  59.         move.w        pf_totprint(sp),d0
  60.         ext.l        d0                                ;return d0 (long)
  61. _fpf_thatsall:
  62.         lea            pf_noofvars(sp),sp
  63.         movem.l        (sp)+,d1-d7/a0-a6
  64.         rts
  65. ; a3 pf_variable table, d0 character to add to the buffer
  66. P_DoChar
  67.         move.w        d1,-(sp)
  68.         move.w        pf_stringpos(a3),d1
  69.         move.b        d0,(a3,d1.w)
  70.         if            eq,p_dc_dit
  71.         addq.w        #1,pf_totprint(a3)
  72.         addq.w        #1,d1
  73.         if.w        d1,lt,#BUFSIZE,p_dc_ok
  74. p_dc_dit
  75.         bsr.s        P_dump
  76.         moveq        #0,d1
  77. p_dc_ok    move.w        d1,pf_stringpos(a3)
  78.         move.w        (sp)+,d1
  79.         rts
  80. ; Pass with d1 as the length to print, a3 with the pf_variable table
  81. P_dump    movem.l        d0-d7/a0-a6,-(sp)
  82.         if.b        pf_badoutput(a3),pdump_nomore
  83.         moveq        #0,d3
  84.         move.w        d1,d3                            ;d3 <== length
  85.         beq.s        pdump_nomore
  86.         move.l        a3,d2                            ;d2 <== buffer
  87.         move.l        pf_output(a3),d1                ;d1 <== file handle
  88.         LVO            pf_DOSBase(a3),Write
  89.         tst.w        d0
  90.         seq.b        pf_badoutput(a3)                ;didn't write anything?
  91. pdump_nomore:
  92.         movem.l        (sp)+,d0-d7/a0-a6
  93.         rts
  94.  
  95. *********************************************************************
  96. _spf ; outstring, (long)maxlen, string, args, ...
  97. *********************************************************************
  98.                 rsset        0
  99. sf_stringpos    rs.w    1                            ;current position
  100. sf_pad            rs.w    1
  101. sf_noofvars        rs.l    0
  102. sf_stacked        rs.l    14
  103. sf_return        rs.l    1
  104. ; CALLER MUST SUPPLY THE FOLLOWING ON THE STACK
  105. sf_outbuf        rs.l    1                            ;output buffer
  106. sf_pad2            rs.w    1
  107. sf_maxlen        rs.w    1                            ;0 or less = unlimited
  108. sf_string        rs.l    1                            ;format
  109. sf_stream        rs.l    0                            ;start of the stream
  110. *********************************************************************
  111.         movem.l        d1-d7/a0-a6,-(sp)
  112.         subq.l        #sf_noofvars,sp
  113.         move.l        sp,a3                            ;a3 <== base of data
  114.         subq.w        #1,sf_maxlen(a3)                ;compensate
  115.         clr.w        sf_stringpos(a3)                ;buf pos
  116.         move.l        sf_string(a3),a0
  117.         move.l        a0,d7
  118.         beq.s        _spf_nomore                        ;NULL string?
  119.         lea            sf_stream(a3),a1
  120.         lea            S_DoChar(pc),a2                    ;call this each char
  121.         LVOEXEC        RawDoFmt
  122.         move.w        sf_stringpos(a3),d0
  123.         move.l        sf_outbuf(a3),a0                ;Terminate with '\0'
  124.         clr.b        (a0,d0.w)
  125.         ext.l        d0                                ;return d0 (long)
  126. _spf_nomore:
  127.         addq.l        #sf_noofvars,sp
  128.         movem.l        (sp)+,d1-d7/a0-a6
  129.         rts
  130. ; a3 sf_variable table, d0 character to add to the buffer
  131. S_DoChar
  132.         movem.l        a0/d1,-(sp)
  133.         move.w        sf_stringpos(a3),d1
  134.         if.w        d1,eq,sf_maxlen(a3),s_nomor        ;stop at len-1
  135.         move.l        sf_outbuf(a3),a0
  136.         if.b        not,d0,s_nomor                    ;write '\0' in later
  137.         addq.w        #1,sf_stringpos(a3)
  138.         move.b        d0,(a0,d1.w)
  139. s_nomor    movem.l        (sp)+,a0/d1
  140.          rts
  141.